//
//  CYNet.m
//  CYTest
//
//  Created by Rain on 16/6/21.
//  Copyright © 2016年 Rain. All rights reserved.
//

#import "CYNet.h"

__strong static AFHTTPSessionManager *AFHTTPMgr;
__strong static CYNet *NetInstance=nil;

@implementation CYNet

+(CYNet *)getInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        NetInstance= [[CYNet alloc]init];//初始化实例
        //一下是AFHTTPOerrationManager的配置
        AFHTTPMgr=[AFHTTPSessionManager manager];
        //申明返回的结果是json类型
        AFHTTPMgr.responseSerializer=[AFJSONResponseSerializer serializer];
        //申明请求的数据是json类型
        AFHTTPMgr.requestSerializer=[AFJSONRequestSerializer serializer];
        //如果报接受类型不一致请替换一致text/xml或别的
        //AFHTTPMgr.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/xml"];
        //设置超时时间
        AFHTTPMgr.requestSerializer.timeoutInterval=5;
    });
    return NetInstance;
}

-(void)getWeatherWithCityName:(NSString *)cityName
{
    //接口地址
    NSString *url=[NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather"];
    //参数
    NSDictionary *parameters=[[NSDictionary alloc]initWithObjectsAndKeys:cityName,@"q", nil];
    //发请求
    [AFHTTPMgr GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        //请求成功Block
        //将返回数据传入代理方法
        [self.delegate getWeatherInfoSuccessFeedback:responseObject];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        //请求失败Blick
        //将错误信息传入代理方法
        [self.delegate getWeatherInfoFailFeedback:error];
    }];
    
}
@end
